﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Remote_server
{
    /// <summary>
    /// Handles basic media player functionality
    /// </summary>
    public static class MediaHandler
    {
        [DllImport("user32.dll", EntryPoint = "keybd_event", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern void keybd_event(byte vk, byte scan, int flags, int extrainfo);

        /// <summary>
        /// Mutes the system if its not already muted.
        /// If muted, it is unmuted
        /// </summary>
        public static void Mute()
        {
            keybd_event(0xAD, 0, 0, 0);
            keybd_event(0xAD, 0, 2, 0);
        }

        /// <summary>
        /// Plays the next song in a playlist
        /// </summary>
        public static void PlayNext()
        {
            keybd_event(0xB0, 0, 0, 0);
            keybd_event(0xB0, 0, 2, 0);
        }

        /// <summary>
        /// Plays the previous song in the playlist
        /// </summary>
        public static void PlayPrev()
        {
            keybd_event(0xB1, 0, 0, 0);
            keybd_event(0xB1, 0, 2, 0);
            keybd_event(0xB1, 0, 0, 0);
            keybd_event(0xB1, 0, 2, 0);
        }

        /// <summary>
        /// Increases the system volume a certain amount
        /// </summary>
        /// <param name="amount">How much to raise the volume</param>
        public static void VolumeUp(int amount)
        {
            for (int i = 0; i <= amount; i++)
            {
                keybd_event(0xAF, 0, 0, 0);
                keybd_event(0xAF, 0, 2, 0);
            }
        }

        /// <summary>
        /// Decreases the system volume a certain amount
        /// </summary>
        /// <param name="amount">How much to lower the volume</param>
        public static void VolumeDown(int amount)
        {
            for (int i = 0; i <= amount; i++)
            {
                keybd_event(0xAE, 0, 0, 0);
                keybd_event(0xAE, 0, 2, 0);
            }
        }

        /// <summary>
        /// Pauses the current song, if already paused, nothing will happen
        /// </summary>
        public static void Pause()
        {
            keybd_event(0xB2, 0, 0, 0);
            keybd_event(0xB2, 0, 2, 0);
           
        }

        /// <summary>
        /// Resume playing the current song.
        /// If already playing, the song will be paused
        /// </summary>
        public static void Play()
        {
            keybd_event(0xB3, 0, 0, 0);
            keybd_event(0xB3, 0, 2, 0);
        }
    }
}